# plumber.R
# install.packages("jsonlite")

library(readr)
library(readxl)
library(haven)
library(multcomp)
library(psych)
library(CBCgrps)
library(car)
library(dplyr)
library(gplots)
library(corrplot)
library(vcd)
library(tableone)
library(broom)
library(survival)

library(plyr) #json转数据框


library(multcomp)
library("RJSONIO")
library(jsonlite)



#library(RMySQL)

#' Echo the parameter that was sent in 
#' @serializer unboxedJSON
#' @param msg The message to echo back.
#' @get /echo
#' 
function(msg=""){
  data_sample <- cholesterol
  as.data.frame(data_sample)
  
  is.data.frame(data_sample)
 return(data_sample)
}


getColumn<- function(dataFrame){
  true()
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /echo1
#' 
function(req,res,name=""){
  dataFrame<-as.data.frame(fromJSON(req$postBody))
  #转为因子   水平就是因子的不同取值，R中一般把分类变量作为因子变量
  #dataFrame[sapply(dataFrame, is.character)] <- lapply(dataFrame[sapply(dataFrame, is.character)],as.factor)
  #dataFrame[sapply(dataFrame, getColumn)] <- lapply(dataFrame[sapply(dataFrame, getColumn)],as.factor)
  
  dataFrame$trt <- as.factor(dataFrame$trt)
  level<-levels(dataFrame$trt)
  print(level)
  res$body <- RJSONIO::toJSON(level)
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @get /FenLeiBianLiangMiaoShuTongJi
#' 分类变量描述性统计
function(req,res,name=""){
  nameVector = unlist(strsplit(name,','))
  dataFrame<-as.data.frame(fromJSON(req$postBody))
  #dataFrame <- getData();
  rec<-list();
  cnt <- 1
  while (cnt < length(nameVector)+1) {
    nameTmp = nameVector[cnt]
    dataFrame[[nameTmp]] <- as.factor(dataFrame[[nameTmp]])
    level <- levels(dataFrame[[nameTmp]])
    data <- table(dataFrame[[nameTmp]])
    tmpList = list(nameTmp,level,data)
    
    #rec[[cnt]] = names(data)
    rec[[cnt]] = tmpList
    cnt = cnt + 1
  }

  json <- RJSONIO::toJSON(rec)

  jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- jsonUTF8
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /LianXuBianLiangMiaoShuTongJi
#' 连续变量描述性统计
function(req,res,name="id"){
  nameVector = unlist(strsplit(name,','))
  dataFrame <- getData();
  rec<-list();
  cnt <- 1
  while (cnt < length(nameVector)+1) {
    nameTmp = nameVector[cnt]
    library(psych)
    data <-psych::describe(dataFrame[[nameTmp]])
    tmpList = list(names(data),data)
    rec[[cnt]] = tmpList
    cnt = cnt + 1
  }
  json <- RJSONIO::toJSON(rec)
  jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- jsonUTF8
  res
}


#' Echo the parameter that was sent in
#' @param name  
#' @post /fenZuTongJi
#' 分组统计
function(req,res,name="trt"){
  dataFrame<-as.data.frame(fromJSON(req$postBody))
  #dataFrame <-jsonlite::fromJSON(paste(readLines("F:\\RWork\\statistics\\data.json"), collapse=""))
  library(psych)
  groupData <- psych::describeBy(dataFrame, dataFrame[[name]])
  
  
  rec<-list();
  cnt <- 1
  while (cnt < length(groupData)+1) {
    rec[[cnt]] <- groupData[cnt]
    cnt = cnt + 1
  }
  
  print(rec)
  res$body <- jsonlite::toJSON(rec)
  res
}



requestJson<-function(req){
  requstData = req$postBody
  sysName <- as.data.frame(Sys.info())[1,1]
  if(sysName == "Windows"){
    requstData<-iconv(requstData,from="UTF8",to="gbk")
  }else if(sysName == "Linux"){
    
  }
  dataFrame <-as.data.frame(fromJSON(requstData))
  return(dataFrame)
}


responseJson<-function(res,data){
  print(data)
  json <- jsonlite::toJSON(data)
  #jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- json
  return(res)
}



#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /lianXuMiaoShu
#' 连续变量描述性统计
function(req,res,variableCode){
  dataFrame<-as.data.frame(fromJSON(req$postBody))
  #name="response"
  #library(multcomp)
  #dataFrame <- cholesterol
  
  dataFrame[[variableCode]]=as.numeric(unlist(dataFrame[[variableCode]]))
  library(psych)
  data <- psych::describe(dataFrame[[variableCode]])
  res$body <- jsonlite::toJSON(data)
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fenLeiMiaoShu
#' 分类变量描述性统计
function(req,res,variableCode){
  requstData = req$postBody
  sysName <- as.data.frame(Sys.info())[1,1]
  if(sysName == "Windows"){
    requstData<-iconv(requstData,from="UTF8",to="gbk")
  }else if(sysName == "Linux"){
    
  }
  dataFrame<-as.data.frame(fromJSON(requstData))
  
  #variableCode="trt"
  #library(multcomp)
  #dataFrame <- cholesterol
  dataFreq<- as.data.frame(addmargins(table(dataFrame[[variableCode]]))) 
  dataPercent <- as.data.frame(round(prop.table(table(dataFrame[[variableCode]])),2)*100) # 计算构成比
  dataMerge <- merge(dataFreq,dataPercent,by="Var1")
  
  data<- as.data.frame(table(dataFrame[[variableCode]]))
  library(epiR)
  ci<- cbind(data$Freq[],dataFreq$Freq[length(dataFreq$Freq)]) #创建可信区间计算矩阵
  epi <-epi.conf(ci,ctype='prevalence',method='clopper-pearson',conf.level = 0.95)  #计算可信区间
  epi$lower<-round(epi$lower*100,2)
  epi$upper<-round(epi$upper*100,2)
  
  dataMerge <- cbind(dataMerge,epi)
  json <- jsonlite::toJSON(dataMerge)
  #jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- json
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /zhengTaiJianyan
#' 正态性检验
function(req,res,variableCode){
  requstData = req$postBody
  sysName <- as.data.frame(Sys.info())[1,1]
  if(sysName == "Windows"){
    requstData<-iconv(requstData,from="UTF8",to="gbk")
  }else if(sysName == "Linux"){
    
  }
  dataFrame <-as.data.frame(fromJSON(requstData))
  testData <-as.numeric(unlist(dataFrame[[variableCode]]))
  
  # variableCode="response"
  # library(multcomp)
  # dataFrame <- cholesterol

  
  shapiroTest <-shapiro.test(testData)
  ksTest <-  ks.test(testData, "pnorm")
  tidy_sw_test<-dplyr::rename(tidy(shapiroTest),"值"=statistic,"P值" = p.value)
  ks_test<- dplyr::rename(tidy(ksTest),"值"=statistic,"P值" = p.value)
  tidy_sw_test$method <- 'shapiroTest'
  ks_test$method <- 'ksTest'
  
  if(tidy_sw_test$P值<0.05){
    tidy_sw_test$意义 <- '无意义'
  }else{
    tidy_sw_test$意义 <- '有意义'
  }
  
  if(ks_test$P值<0.05){
    ks_test$意义 <- '无意义'
  }else{
    ks_test$意义 <- '有意义'
  }
  
  rec <- list()
  if(length(dataFrame[[variableCode]])<=2000){
    tidy_sw_test$flag <- 1
    ks_test$flag <- 0

    rec <- list(tidy_sw_test,ks_test)
  }else{
    tidy_sw_test$flag <- 0
    ks_test$flag <- 1
    rec <- list(tidy_sw_test,ks_test)
  }
  json <- jsonlite::toJSON(rec)
  #jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- json
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fenLeiMiaoShu
#' 分类变量描述性统计
function(req,res,variableCode){
  requstData = req$postBody
  sysName <- as.data.frame(Sys.info())[1,1]
  if(sysName == "Windows"){
    requstData<-iconv(requstData,from="UTF8",to="gbk")
  }else if(sysName == "Linux"){
    
  }
  dataFrame<-as.data.frame(fromJSON(requstData))
  
  #variableCode="trt"
  #library(multcomp)
  #dataFrame <- cholesterol
  dataFreq<- as.data.frame(addmargins(table(dataFrame[[variableCode]]))) 
  dataPercent <- as.data.frame(round(prop.table(table(dataFrame[[variableCode]])),2)*100) # 计算构成比
  dataMerge <- merge(dataFreq,dataPercent,by="Var1")
  
  data<- as.data.frame(table(dataFrame[[variableCode]]))
  library(epiR)
  ci<- cbind(data$Freq[],dataFreq$Freq[length(dataFreq$Freq)]) #创建可信区间计算矩阵
  epi <-epi.conf(ci,ctype='prevalence',method='clopper-pearson',conf.level = 0.95)  #计算可信区间
  epi$lower<-round(epi$lower*100,2)
  epi$upper<-round(epi$upper*100,2)
  
  dataMerge <- cbind(dataMerge,epi)
  json <- jsonlite::toJSON(dataMerge)
  #jsonUTF8<-iconv(json,from="gbk",to="UTF8")
  res$setHeader("Content-type", "text/json; charset=UTF8")
  res$body <- json
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fangChaQiXingJianYan
#' 方差齐性检验
function(req,res,fenZuCode,lianXuCode){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode]]=as.numeric(unlist(dataFrame[[lianXuCode]]))
  dataFrame[[fenZuCode]]=as.factor(unlist(dataFrame[[fenZuCode]]))
  levene_Test<- leveneTest(dataFrame[[lianXuCode]]~dataFrame[[fenZuCode]], data=dataFrame)
  tidy_levene_Test<- dplyr::rename(tidy(levene_Test),"F值"=statistic,"自由度"=df,"P值" = p.value)
  
  if(tidy_levene_Test$P值 > 0.05){
    tidy_levene_Test$意义 <- '方差齐'
  }else{
    tidy_levene_Test$意义 <- '方差不齐'
  }
  responseJson(res,tidy_levene_Test[c(1,3,2,5)])
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /zhiHeJianYan
#' 秩和检验
function(req,res,fenZuCode,lianXuCode,fenZuType){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode]]=as.numeric(unlist(dataFrame[[lianXuCode]]))
  
  if(fenZuType == 3)
  {
    dataFrame[[fenZuCode]]=as.factor(unlist(dataFrame[[fenZuCode]]))
    erFenData <- wilcox.test(dataFrame[[lianXuCode]]~dataFrame[[fenZuCode]],data = dataFrame)
    tidy_test<- dplyr::rename(tidy(erFenData),"值"=statistic,"P值" = p.value)
    
    if(tidy_test$P值>0.05){
      tidy_test$意义 <- '组间差异无统计学意义'
    }else{
      tidy_test$意义 <- '组间差异有统计学意义'
    }
    print(tidy_test)
    responseJson(res,tidy_test[c(1,2,5)])
  }else if(fenZuType == 4||fenZuType == 5){
    duoFenData <- kruskal.test(dataFrame[[lianXuCode]],dataFrame[[fenZuCode]])
    tidy_test<- dplyr::rename(tidy(duoFenData),"值"=statistic,"P值" = p.value)
    
    if(tidy_test$P值>0.05){
      tidy_test$意义 <- '组间差异无统计学意义'
    }else{
      tidy_test$意义 <- '组间差异有统计学意义'
    }
    responseJson(res,tidy_test[c(1,2,5)])
  }
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /TJianYan
#' T检验
function(req,res,fenZuCode,lianXuCode,testType){
  dataFrame <- requestJson(req)
  
  if(testType=="true"){
    testType=TRUE
  }else{
    testType=FALSE
  }
  
  dataFrame[[lianXuCode]]=as.numeric(unlist(dataFrame[[lianXuCode]]))
  print(dataFrame[[fenZuCode]])
  tt_test<- t.test(dataFrame[[lianXuCode]]~dataFrame[[fenZuCode]], data = dataFrame, var.equal = testType)
  tidy_tt_test<- dplyr::rename(tidy(tt_test),"值"=statistic,"P值" = p.value,"自由度"=parameter)
  
  if(tidy_tt_test$P值>0.05){
    tidy_tt_test$意义 <- '组间差异无统计学意义'
  }else{
    tidy_tt_test$意义 <- '组间差异有统计学意义'
  }
  responseJson(res,tidy_tt_test[c(4,6,5,11)])
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fangChaFenXi
#' 方差分析
function(req,res,fenZuCode,lianXuCode){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode]]=as.numeric(unlist(dataFrame[[lianXuCode]]))
  fit <- aov(dataFrame[[lianXuCode]]~dataFrame[[fenZuCode]],dataFrame)
  tidy_fit<-dplyr::rename(tidy(fit),"值"=statistic,"自由度"=df,"P值" = p.value)
  print(tidy_fit)
  tidy_fit_tmp <- tidy_fit[1,]
     
   if(tidy_fit_tmp$P值>0.05){
     tidy_fit_tmp$意义 <- '组间差异无统计学意义'
   }else{
     tidy_fit_tmp$意义 <- '组间差异有统计学意义'
   }
  
  responseJson(res,tidy_fit_tmp[c(5,2,6,7)])
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /kaFangOrFisher
#' 卡方检验或者Fisher精确概率法
function(req,res,fenZuCode1,fenZuCode2){
  dataFrame <- requestJson(req)
  dataFrame[[fenZuCode1]] <- as.factor(unlist(dataFrame[[fenZuCode1]]))
  dataFrame[[fenZuCode2]] <- as.factor(unlist(dataFrame[[fenZuCode2]]))
  
  data <- table(dataFrame[[fenZuCode1]],dataFrame[[fenZuCode2]])
  chisqTest <- chisq.test(data,correct = FALSE)
  

  way <- "";
  if(sum(chisqTest$observed)>40 && Xsq$expected>5){
    way <- "卡方检验"
  }else{
    way <- "Fisher精确概率法"
  }
  responseJson(res,way)
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /kaFangJianYan
#' 卡方检验
function(req,res,fenZuCode1,fenZuCode2){
  dataFrame <- requestJson(req)
  dataFrame[[fenZuCode1]] <- as.factor(unlist(dataFrame[[fenZuCode1]]))
  dataFrame[[fenZuCode2]] <- as.factor(unlist(dataFrame[[fenZuCode2]]))
  
  data <- table(dataFrame[[fenZuCode1]],dataFrame[[fenZuCode2]])
  chisqTest <- chisq.test(data,correct = FALSE)
  renameColName <-dplyr::rename(tidy(chisqTest),"检测名"=method,"自由度"=parameter,"统计量"=statistic,"P值" = p.value)
  responseJson(res,renameColName)
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fisherTest
#' Fisher精确概率法
function(req,res,fenZuCode1,fenZuCode2){
  dataFrame <- requestJson(req)
  dataFrame[[fenZuCode1]] <- as.factor(unlist(dataFrame[[fenZuCode1]]))
  dataFrame[[fenZuCode2]] <- as.factor(unlist(dataFrame[[fenZuCode2]]))
  data <- table(dataFrame[[fenZuCode1]],dataFrame[[fenZuCode2]])
  fisherTest <- fisher.test(data)
  
  print(fisherTest)
  renameColName <-dplyr::rename(tidy(fisherTest),"检测名"=method,"P值" = p.value)
  responseJson(res,renameColName)
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /lianXuXiangGuanPearson
#' 连续变量相关性 pearson
function(req,res,lianXuCode1,lianXuCode2){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode1]] <- as.numeric(unlist(dataFrame[[lianXuCode1]]))
  dataFrame[[lianXuCode2]] <- as.numeric(unlist(dataFrame[[lianXuCode2]]))
  
  data <- cbind(dataFrame[[lianXuCode1]],dataFrame[[lianXuCode2]])
  print(data)
  library(psych)
  cor <-corr.test(data,method = "pearson")
  data<-data.frame('r'=cor$r, 'p'=cor$p)
  responseJson(res,data)
  res
}

#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /lianXuXiangGuanSpearman
#' 连续变量相关性 spearman
function(req,res,lianXuCode1,lianXuCode2){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode1]] <- as.numeric(unlist(dataFrame[[lianXuCode1]]))
  dataFrame[[lianXuCode2]] <- as.numeric(unlist(dataFrame[[lianXuCode2]]))
  
  data <- cbind(dataFrame[[lianXuCode1]],dataFrame[[lianXuCode2]])
  print(data)
  library(psych)
  cor <-corr.test(data,method = "spearman")
  data<-data.frame('r'=cor$r, 'p'=cor$p)
  responseJson(res,data)
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /fenZuBianLiangXiangGuanXing
#' 分组变量相关性 
function(req,res,fenZuCode1,fenZuCode2){
  dataFrame <- requestJson(req)
  library(vcd)
  cor_value <- assocstats(table(dataFrame[[fenZuCode1]],dataFrame[[fenZuCode1]]))
  data<-data.frame('列联系数'=cor_value$contingency,'Phi系数'=cor_value$phi, 'Cramer’s系数'=cor_value$cramer)
  responseJson(res,data)
  res
}


getDataFrameIndex <-function(dataFrame,name){
  colNameList <-  colnames(dataFrame)
  count <- 1
  while (count < length(colNameList)+1) {
    if(colNameList[count] == name)
    {
      return(count)
    }
    count <- count + 1 
  }
  return(0)
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /luoJiHuiGui
#' 逻辑回归 
function(req,res,fenZuCode_y,lianXuCode_x="",fenZuCode_x=""){
  dataFrame <- requestJson(req)
  library(tableone)
  library(broom)
  
  dataFrame[[fenZuCode_y]] <- as.factor(unlist(dataFrame[[fenZuCode_y]]))
  index <- c(getDataFrameIndex(dataFrame,fenZuCode_y))
  
  if(!is.null(lianXuCode_x) &&!is.na(lianXuCode_x))
  {
    lianXuNameVector = unlist(strsplit(lianXuCode_x,','))
    lianXuCount <- 1
    while (lianXuCount < length(lianXuNameVector)+1) {
      nameTmp = lianXuNameVector[lianXuCount]
      dataFrame[[nameTmp]] <- as.numeric(unlist(dataFrame[[nameTmp]]))
      index <- c(index,getDataFrameIndex(dataFrame,nameTmp))
      lianXuCount = lianXuCount + 1
    }
  }
  
  if(!is.null(fenZuCode_x) &&!is.na(fenZuCode_x))
  {
    fenZuNameVector = unlist(strsplit(fenZuCode_x,','))
    fenZuCount <- 1
    while (fenZuCount < length(fenZuNameVector)+1) {
      nameTmp = fenZuNameVector[fenZuCount]
      dataFrame[[nameTmp]] <- as.factor(unlist(dataFrame[[nameTmp]]))
      index <- c(index,getDataFrameIndex(dataFrame,nameTmp))
      fenZuCount = fenZuCount + 1
    }
  }
  
  dataFrameTmp <- dataFrame[,index]
  log_fit <- glm(dataFrameTmp[[fenZuCode_y]]~., family = binomial(link = "logit"), data = dataFrameTmp)
  summary(log_fit)
  returnData<-data.frame('系数'=log_fit$coefficients,'标准误'=tidy(log_fit)$std.error, '统计值'=tidy(log_fit)$statistic,'P值'=tidy(log_fit)$p.value)
  responseJson(res,returnData)
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /xianXingHuiGui
#' 线性回归 
function(req,res,lianXuCode_y,lianXuCode_x="",fenZuCode_x=""){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode_y]] <- as.numeric(unlist(dataFrame[[lianXuCode_y]]))
  index <- c(getDataFrameIndex(dataFrame,lianXuCode_y))
  
  if(!is.null(lianXuCode_x) &&!is.na(lianXuCode_x))
  {
    lianXuNameVector = unlist(strsplit(lianXuCode_x,','))
    lianXuCount <- 1
    while (lianXuCount < length(lianXuNameVector)+1) {
      nameTmp = lianXuNameVector[lianXuCount]
      dataFrame[[nameTmp]] <- as.numeric(unlist(dataFrame[[nameTmp]]))
      index <- c(index,getDataFrameIndex(dataFrame,nameTmp))
      lianXuCount = lianXuCount + 1
    }
  }
  
  if(!is.null(fenZuCode_x) &&!is.na(fenZuCode_x))
  {
    fenZuNameVector = unlist(strsplit(fenZuCode_x,','))
    fenZuCount <- 1
    while (fenZuCount < length(fenZuNameVector)+1) {
      nameTmp = fenZuNameVector[fenZuCount]
      dataFrame[[nameTmp]] <- as.factor(unlist(dataFrame[[nameTmp]]))
      index <- c(index,getDataFrameIndex(dataFrame,nameTmp))
      fenZuCount = fenZuCount + 1
    }
  }
  
  dataFrameTmp <- dataFrame[,index]
  lmreg <- lm(dataFrameTmp[[lianXuCode_y]]~., data=dataFrameTmp)
  summary(lmreg)
  returnData<-data.frame('x类别'=tidy(lmreg)$term ,'系数'=tidy(lmreg)$estimate, '标准误'=tidy(lmreg)$std.error,'P值'=tidy(lmreg)$p.value,'统计值'=tidy(lmreg)$statistic)
  responseJson(res,returnData)
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /shenCunFenXi
#' 生存分析
function(req,res,shengCunShiJianCode,fenZhuCode,Code_x,codeType_x){
  dataFrame <- requestJson(req)
  
  dataFrame[[shengCunShiJianCode]] <- as.numeric(unlist(dataFrame[[shengCunShiJianCode]]))
  dataFrame[[fenZhuCode]] <- as.numeric(unlist(dataFrame[[fenZhuCode]]))
  if(codeType_x=="1")
  {
    dataFrame[[Code_x]] <- as.numeric(unlist(dataFrame[[Code_x]]))
  }else if(codeType_x=="2"){
    dataFrame[[Code_x]] <- as.numeric(unlist(dataFrame[[Code_x]]))
  }
  library(survival)
  fit <- survfit(Surv( dataFrame[[shengCunShiJianCode]], dataFrame[[fenZhuCode]])~dataFrame[[Code_x]], data=dataFrame)

  returnData <- list()
  allCount <- count(dataFrame)
  returnData$总记录数 <- allCount
  print(returnData)
  #levels(dataFrame[[fenZhuCode]])[1])  如果分组数据是因子采用这种方式  
  #dataFrame[[fenZhuCode]][1]
  eventCount <- count(dataFrame %>% filter(dataFrame[[fenZhuCode]]==dataFrame[[fenZhuCode]][1]))
  returnData$发生事件数 <- eventCount
  a<- as.data.frame(summary(fit)$table)
  returnData$中位生存时间 <- a['median']
  returnData$生存时间下限 <- a['0.95LCL']
  responseJson(res,returnData)
  res
}






#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @post /XiangGuanFenXi
#' 相关分析
function(req,res,fenZuCode,lianXuCode){
  dataFrame <- requestJson(req)
  dataFrame[[lianXuCode]]=as.numeric(unlist(dataFrame[[lianXuCode]]))
  fit <- aov(dataFrame[[lianXuCode]]~dataFrame[[fenZuCode]],dataFrame)
  tidy_fit<-dplyr::rename(tidy(fit),"值"=statistic,"自由度"=df,"P值" = p.value)
  responseJson(res,tidy_fit[length(tidy_fit$term)-1,c(length(tidy_fit)-1,2,length(tidy_fit))])
  res
}


#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @get /getMsql
#' 连接数据库
#getData <-  function(){
#  library(RMySQL)
#  conn <- dbConnect(MySQL(), dbname = "clinical", username="root", password="123456", host="152.136.182.96", port=13306)
  # 专治中文乱码
#  dbSendQuery(conn, "SET NAMES gbk")
  #dbWriteTable(conn, "tablename", data) #写表
#  #data <- dbReadTable(conn, "t_followup_dict")  #读表
  
  #查询数据
#  data <- dbGetQuery(conn, "select * from t_statistic_person")
#  dbDisconnect(conn) #关闭连接
#  return(data)
#}


